//
//  OMOrderButtonActionHelper.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/15.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderButtonActionHelper.h"
#import "OMOrder.h"
#import "MainConfigure.h"
@implementation OMOrderButtonActionHelper

#define kBEQ(TITLE) [button.currentTitle isEqualToString:TITLE]
+ (void)actionWithButton:(UIButton *)button Order:(OMOrder *)order
{
    @try {
        if (kBEQ(@"去支付"))
        {
            NaviPush(@"KTLemonPaymentViewController", (@{@"title":@"支付",@"order":order,@"isRepay":@(YES)}));
        }
        else if (kBEQ(@"取消订单"))
        {
            [KTAlertView alertWithMessage:order.paymentMethodType?@"此订单无款退。":@"此订单有款退,款项将退回到您的付款账户。"
                                    Title:@"确定取消订单吗？"
                             ButtonTitles:@[@"确定"]
                           withClickBlock:^(NSUInteger indexOfButton) {
                               if (indexOfButton) {
                                   [self orderCancelWithOrder:order];
                               }
                           }];
        }
        else if (kBEQ(@"确认收货"))
        {
            [KTAlertView alertWithMessage:@"您确认已接收到订单所指的商品吗？"
                                    Title:@"确认收货吗？"
                             ButtonTitles:@[@"确认"]
                           withClickBlock:^(NSUInteger indexOfButton) {
                               if (indexOfButton) {
                                   [self orderGoodReceiveWithOrder:order];
                               }
                           }];
        }
        else if (kBEQ(@"删除订单"))
        {
            [KTAlertView alertWithMessage:@"删除订单后，将无法在订单列表查看。"
                                    Title:@"确定删除订单吗？"
                             ButtonTitles:@[@"确定"]
                           withClickBlock:^(NSUInteger indexOfButton) {
                               if (indexOfButton) {
                                   [self orderDeleteWithOrder:order];
                               }
                           }];
        }
        else if (kBEQ(@"评价"))
        {
            NaviPush(@"OMOrderEvaluateSubmitViewController", @{@"order":order});
        }
        else if (kBEQ(@"查看评价"))
        {
            NaviPush(@"OMOrderEvaluateDetailViewController", @{@"order":order});
        }
        else if (kBEQ(@"申请退款"))
        {
            NaviPush(@"OMOrderRefundSubmitViewController", @{@"order":order});
        }
        else if (kBEQ(@"查看退款"))
        {
            NaviPush(@"OMOrderRefundDetailViewController", @{@"refundId":order.refundsId});
        }
        else if (kBEQ(@"意见反馈"))
        {
            NaviPush(@"OMAdviseSubmitViewController", @{@"title":@"意见反馈"});
        }
    } @catch (NSException *exception) {
        NSLog(@"%@",exception);
        [OMHUD HUDInfo:@"操作失败"];
    } @finally {

    }

}


/**
 确认收货请求

 @param order 订单实体类
 */
+ (void)orderGoodReceiveWithOrder:(OMOrder*)order
{
    [OMHUD HUDShieldLoading:@"正在确认收货"];
    [KTHttpUtils POST:@"member/order/receive.jhtml"
           parameters:@{@"sn":order.sn}
             progress:nil
           completion:^(id responseObject, BOOL isSuccess, NSString *message) {
        [OMHUD HUDHide];
        if (isSuccess) {
            SMNotificationPost(OMORDER_NEED_REFRESH_NOTIFICATION, nil);
        }else {
            [KTAlertView alertWithMessage:message.length?message:@"订单状态可能改变了哦，请刷新列表后重试。"
                                    Title:@"确认收货失败!"
                             ButtonTitles:nil
                           withClickBlock:nil];
        }
    }];
}

/**
 订单取消请求

 @param order 订单实体类
 */
+ (void)orderCancelWithOrder:(OMOrder *)order
{
    [OMHUD HUDShieldLoading:@"正在取消订单"];
    [KTHttpUtils POST:@"member/order/cancel.jhtml"
           parameters:@{@"sn":order.sn}
             progress:nil
           completion:^(id responseObject, BOOL isSuccess, NSString *message) {
        [OMHUD HUDHide];
        if (isSuccess) {
            SMNotificationPost(OMORDER_NEED_REFRESH_NOTIFICATION, nil);
            NaviPresent(@"OMOrderCancelSubmitViewController", (@{@"isPresented":@(YES),@"order":order}));
        }else {
            [KTAlertView alertWithMessage:message.length?message:@"订单状态可能改变了哦，请刷新列表后重试。"
                                    Title:@"订单取消失败!"
                             ButtonTitles:nil
                           withClickBlock:nil];
        }
    }];
}


+ (void)orderDeleteWithOrder:(OMOrder*)order
{
    [OMHUD HUDShieldLoading:@"正在删除订单"];
    [KTHttpUtils POST:@"member/order/delete.jhtml"
           parameters:@{@"sn":order.sn}
             progress:nil
           completion:^(id responseObject, BOOL isSuccess, NSString *message) {
               [OMHUD HUDHide];
               if (isSuccess) {
                   [OMHUD HUDSuccess:@"订单删除成功"];
                   SMNotificationPost(OMORDER_NEED_REFRESH_NOTIFICATION, nil);
               }else {
                   [KTAlertView alertWithMessage:message.length?message:@"订单状态可能改变了哦，请刷新列表后重试。"
                                           Title:@"订单删除失败!"
                                    ButtonTitles:nil
                                  withClickBlock:nil];
               }
           }];
}
@end
